package com.example.demo.utility;


import lombok.experimental.UtilityClass;
import net.sf.json.JSONObject;
import org.apache.tomcat.util.http.fileupload.RequestContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义工具类
 *
 * @author yzh
 * @date 2022/12/13
 */
@UtilityClass
public class CustomUtil {

    /**
     * 获取request中所有的参数
     */
    public StringBuffer get_request_all() {

        /*        Enumeration enu = request.getParameterNames();
        while (enu.hasMoreElements()) {
            String paraName = (String) enu.nextElement();
            System.out.println(paraName + ": " + request.getParameter(paraName));
        }*/

        StringBuffer t = new StringBuffer();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Enumeration enu = request.getParameterNames();
        while (enu.hasMoreElements()) {
            String paraName = (String) enu.nextElement();
            t.append(paraName + ": " + request.getParameter(paraName) + "\n");
            /* System.out.println(paraName + ": " + request.getParameter(paraName));*/
        }
        if (!isEmpty(t)) {
            return t;
        } else {
            return t.append("request为空");
        }

    }

    /**
     * 判断String是否为空
     */
    public Boolean isEmpty(String... strs) {
        boolean flag = false;
        for (String str : strs) {
            if (str == null || "".equals(str.trim())) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * 判断StringBuffer是否为空
     */
    public Boolean isEmpty(StringBuffer str) {
        boolean flag = false;
        if (str == null || str.length() <= 0) {
            flag = true;

        }
        return flag;
    }


    /**
     * 获得文件名后缀
     *
     * @param filePath 文件路径
     * @return {@link String}
     */
    public static String getFileNameSuffix(String filePath) {

        if (filePath == null || filePath.lastIndexOf(".") == -1) {
            //文件没有后缀名的情况,直接返回空
            return "";
        }
        //获取filePath最后一个.后面的字符串当后缀
        return filePath.substring(filePath.lastIndexOf("."));
    }


    /**
     * 获取url域名
     *
     * @param url url
     * @return {@link String}
     */
    public String getUrlDomain(String url) {
        Pattern compile = Pattern.compile("[^//]*?\\.(com|cn|net|org)", Pattern.CASE_INSENSITIVE);//Pattern.CASE_INSENSITIVE 忽略大小写
        Matcher matcher = compile.matcher(url);
        matcher.find();

        return matcher.group();
    }

    /**
     * @author yzh
     * @time 2023/3/27
     * GET/POST连接
     */
    public static String doPost(String httpurl, Map<String, Object> param, String type) {
        String jsonparam = JSONObject.fromObject(param).toString();

        StringBuffer sb = new StringBuffer();
        try {
            URL url = new URL(httpurl);
            // 获取httpurlConnection连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置读取超时
            conn.setConnectTimeout(3000);
            // 设置读取超时
            conn.setReadTimeout(3000);

            // 传送数据
            conn.setDoOutput(true);
            // 读取数据
            conn.setDoInput(true);
            // 设置请求方式
            conn.setRequestMethod(type);
            // 设置传入参数格式
            conn.setRequestProperty("Content-Type", "application/json");
            // 获取输出流
            OutputStream os = conn.getOutputStream();
            // 输出数据
            os.write(jsonparam.getBytes("UTF-8"));
            // 获取输入流
            InputStream is = conn.getInputStream();
            // 封装输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));


            String line = null;

            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append("\r\n");
            }
            if (null != br) {
                br.close();
            }
            if (null != is) {
                is.close();
            }
            if (null != os) {
                os.close();
            }
            // 关闭连接
            conn.disconnect();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        //System.out.println("返回："+sb.toString());
        return sb.toString();
    }


}
